package org.dding.mvc.web.tag;


import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MapTag extends SimpleTagSupport {

	private String value;
	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	private String var;

	@Override
	public void doTag() throws JspException {

		ServletRequest request = ((PageContext) getJspContext()).getRequest();
		StringTokenizer token = new StringTokenizer(value, ",");
		Map<String, Object> result = new HashMap<String, Object>();
		while (token.hasMoreTokens()) {
			String pair = token.nextToken();
			String[] temp = pair.split(":");
			if(temp!=null&&temp.length==1)
				result.put(temp[0], temp[0]);
			if(temp!=null&&temp.length==2)
				result.put(temp[0], temp[1]);
		}
		request.setAttribute(var, result);
	}

}
